package com.qmx.smedicinebox.sys.controller;

import com.qmx.smedicinebox.constant.Constant;
import com.qmx.smedicinebox.sys.entity.MedicationSituationEntity;
import com.qmx.smedicinebox.sys.entity.MedicineEntity;
import com.qmx.smedicinebox.sys.service.MedicationSituationService;
import com.qmx.smedicinebox.utils.DateTimeUtil;
import com.qmx.smedicinebox.utils.PageUtils;
import com.qmx.smedicinebox.utils.R;
import com.qmx.smedicinebox.vo.MSVo;
import com.qmx.smedicinebox.vo.MedicationSituationVo;
import com.qmx.smedicinebox.vo.MessageVo;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用药情况表
 *
 * @author xiaozhiyong
 * @email xiaozhiyong03@gmail.com
 * @date 2024-03-26 20:55:57
 */
@RestController
@RequestMapping("smb/medicationsituation")
public class MedicationSituationController {
    @Autowired
    private MedicationSituationService medicationSituationService;

    /**
     * 列表
     */
    @PostMapping("/listpage")
    public R listpage(@RequestBody Map<String, Object> params){
        PageUtils page = medicationSituationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{sId}")
    public R info(@PathVariable("sId") Integer sId){
		MedicationSituationEntity medicationSituation = medicationSituationService.getById(sId);

        return R.ok().put("medicationSituation", medicationSituation);
    }

//    @GetMapping("/list")
//    public R list(){
//        List<MedicationSituationEntity> list = medicationSituationService.list();
//
//        return R.ok().put("list", list);
//    }

    @GetMapping("/list/{userId}/{date}")
    @Operation(summary = "获取记录",description = "传入userId和日期字符串")
    public R medicationSituationList(@PathVariable("userId") Integer userId,
                                     @PathVariable("date") String date){
        String s = DateTimeUtil.addLeadingZeros(date, Constant.HYPHEN);
        R result = medicationSituationService.medicationSituationList(userId, s);

        return result;
    }
    @GetMapping("/list")
    @Operation(summary = "获取记录",description = "传入userId和日期字符串")
    public R medicationSituationListQuery(@RequestParam Integer userId,
                                          @RequestParam String date){
        String datetime = DateTimeUtil.addLeadingZeros(date, Constant.SLASH);
        String s = DateTimeUtil.TransformStringSlashToDefault(datetime);
        R result = medicationSituationService.medicationSituationList(userId, s);

        return result;
    }

    @PostMapping("/msList")
    @Operation(summary = "获取记录",description = "传入userId和日期字符串")
    public R medicationSituationList(@RequestBody MSVo msVo){

        String datetime = DateTimeUtil.addLeadingZeros(msVo.getDate(), Constant.SLASH);
        String s = DateTimeUtil.TransformStringSlashToDefault(datetime);

        R result = medicationSituationService.medicationSituationList(msVo.getUserId(),s);
        return result;

    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @Operation(summary = "增加记录",description = "传入medicationSituationVo对象")
    public R save(@RequestBody MedicationSituationVo medicationSituationVo){
		medicationSituationService.save(medicationSituationVo);

        return R.ok();
    }

    /**
     * 批量保存
     */
    @PostMapping("/saveBatch")
    public R saveBatch(@RequestBody List<MedicationSituationEntity> medicationSituations){
        medicationSituationService.saveBatch(medicationSituations);
        return R.ok();
    }


    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody MedicationSituationEntity medicationSituation){
		medicationSituationService.updateById(medicationSituation);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Integer[] sIds){
		medicationSituationService.removeByIds(Arrays.asList(sIds));

        return R.ok();
    }

}
